package com.example.redislearning.util;

/**
 * Redis Key生成工具类
 * 统一管理所有Redis key的命名规范
 */
public class RedisKeyUtil {

    private static final String SEPARATOR = ":";
    private static final String PREFIX_USER = "user";
    private static final String PREFIX_COURSE = "course";
    private static final String PREFIX_CART = "cart";
    private static final String PREFIX_LEADERBOARD = "leaderboard";
    private static final String PREFIX_LOCK = "lock";
    private static final String PREFIX_RATE_LIMIT = "rate_limit";
    private static final String PREFIX_LOTTERY = "lottery";
    private static final String PREFIX_NOTIFICATION = "notification";
    private static final String PREFIX_MESSAGE = "message";

    /**
     * 用户信息缓存
     */
    public static String getUserInfoKey(Integer userId) {
        return PREFIX_USER + SEPARATOR + "info" + SEPARATOR + userId;
    }

    /**
     * 用户积分缓存
     */
    public static String getUserPointsKey(Integer userId) {
        return PREFIX_USER + SEPARATOR + "points" + SEPARATOR + userId;
    }

    /**
     * 用户学习时长缓存
     */
    public static String getUserStudyDurationKey(Integer userId) {
        return PREFIX_USER + SEPARATOR + "study_duration" + SEPARATOR + userId;
    }

    /**
     * 课程详情缓存
     */
    public static String getCourseInfoKey(Integer courseId) {
        return PREFIX_COURSE + SEPARATOR + "info" + SEPARATOR + courseId;
    }

    /**
     * 课程销量缓存
     */
    public static String getCourseSalesKey(Integer courseId) {
        return PREFIX_COURSE + SEPARATOR + "sales" + SEPARATOR + courseId;
    }

    /**
     * 课程评分缓存
     */
    public static String getCourseRatingKey(Integer courseId) {
        return PREFIX_COURSE + SEPARATOR + "rating" + SEPARATOR + courseId;
    }

    /**
     * 购物车缓存
     */
    public static String getCartKey(Integer userId) {
        return PREFIX_CART + SEPARATOR + userId;
    }

    /**
     * 购物车商品数量缓存
     */
    public static String getCartItemCountKey(Integer userId) {
        return PREFIX_CART + SEPARATOR + "count" + SEPARATOR + userId;
    }

    /**
     * 积分排行榜
     */
    public static String getPointsLeaderboardKey() {
        return PREFIX_LEADERBOARD + SEPARATOR + "points";
    }

    /**
     * 学习时长排行榜
     */
    public static String getStudyDurationLeaderboardKey() {
        return PREFIX_LEADERBOARD + SEPARATOR + "study_duration";
    }

    /**
     * 完成课程数排行榜
     */
    public static String getCompletedCoursesLeaderboardKey() {
        return PREFIX_LEADERBOARD + SEPARATOR + "completed_courses";
    }

    /**
     * 热门课程排行榜
     */
    public static String getPopularCoursesKey() {
        return PREFIX_LEADERBOARD + SEPARATOR + "popular_courses";
    }

    /**
     * 分布式锁
     */
    public static String getLockKey(String resource) {
        return PREFIX_LOCK + SEPARATOR + resource;
    }

    /**
     * API限流
     */
    public static String getApiRateLimitKey(String apiKey, Integer userId) {
        return PREFIX_RATE_LIMIT + SEPARATOR + "api" + SEPARATOR + apiKey + SEPARATOR + userId;
    }

    /**
     * 用户行为限流
     */
    public static String getUserActionRateLimitKey(String action, Integer userId) {
        return PREFIX_RATE_LIMIT + SEPARATOR + "action" + SEPARATOR + action + SEPARATOR + userId;
    }

    /**
     * IP限流
     */
    public static String getIpRateLimitKey(String ipAddress) {
        return PREFIX_RATE_LIMIT + SEPARATOR + "ip" + SEPARATOR + ipAddress;
    }

    /**
     * 抽奖活动参与者集合
     */
    public static String getLotteryParticipantsKey(String activityId) {
        return PREFIX_LOTTERY + SEPARATOR + "participants" + SEPARATOR + activityId;
    }

    /**
     * 抽奖活动中奖者列表
     */
    public static String getLotteryWinnersKey(String activityId) {
        return PREFIX_LOTTERY + SEPARATOR + "winners" + SEPARATOR + activityId;
    }

    /**
     * 用户每日抽奖次数
     */
    public static String getUserDailyLotteryKey(Integer userId) {
        return PREFIX_LOTTERY + SEPARATOR + "daily" + SEPARATOR + userId;
    }

    /**
     * 用户消息队列
     */
    public static String getUserMessageQueueKey(Integer userId) {
        return PREFIX_MESSAGE + SEPARATOR + "user" + SEPARATOR + userId;
    }

    /**
     * 用户未读消息计数
     */
    public static String getUserUnreadCountKey(Integer userId) {
        return PREFIX_MESSAGE + SEPARATOR + "unread" + SEPARATOR + userId;
    }

    /**
     * 系统公告队列
     */
    public static String getSystemAnnouncementKey() {
        return PREFIX_NOTIFICATION + SEPARATOR + "system";
    }

    /**
     * 课程更新通知
     */
    public static String getCourseUpdateNotificationKey(Integer courseId) {
        return PREFIX_NOTIFICATION + SEPARATOR + "course_update" + SEPARATOR + courseId;
    }

    /**
     * 学习提醒队列
     */
    public static String getStudyReminderKey(Integer userId) {
        return PREFIX_NOTIFICATION + SEPARATOR + "study_reminder" + SEPARATOR + userId;
    }

    /**
     * 促销通知队列
     */
    public static String getPromotionNotificationKey(String promotionId) {
        return PREFIX_NOTIFICATION + SEPARATOR + "promotion" + SEPARATOR + promotionId;
    }

    /**
     * 抽奖活动基本信息
     */
    public static String getLotteryActivityKey(String activityId) {
        return PREFIX_LOTTERY + SEPARATOR + "activity" + SEPARATOR + activityId;
    }

    /**
     * 抽奖活动奖品池
     */
    public static String getLotteryPrizePoolKey(String activityId) {
        return PREFIX_LOTTERY + SEPARATOR + "prize_pool" + SEPARATOR + activityId;
    }


    /**
     * 抽奖活动总奖品剩余数量
     */
    public static String getLotteryTotalPrizeKey(String activityId) {
        return PREFIX_LOTTERY + SEPARATOR + "total_prize" + SEPARATOR + activityId;
    }

    /**
     * 抽奖活动每日奖品剩余数量
     */
    public static String getLotteryDailyPrizeKey(String activityId) {
        return PREFIX_LOTTERY + SEPARATOR + "daily_prize" + SEPARATOR + activityId;
    }

    /**
     * 用户每日抽奖次数
     */
    public static String getUserDailyLotteryKey(Integer userId, String activityId) {
        return PREFIX_LOTTERY + SEPARATOR + "daily" + SEPARATOR + userId + SEPARATOR + activityId;
    }

    /**
     * 用户抽奖历史
     */
    public static String getUserLotteryHistoryKey(Integer userId) {
        return PREFIX_LOTTERY + SEPARATOR + "history" + SEPARATOR + userId;
    }


    /**
     * 用户学习进度
     */
    public static String getUserCourseProgressKey(Integer userId, Integer courseId) {
        return PREFIX_USER + SEPARATOR + "progress" + SEPARATOR + userId + SEPARATOR + courseId;
    }


    /**
     * 用户学习状态key
     */
    public static String getUserLearningKey(Integer userId, Integer courseId) {
        return PREFIX_USER + SEPARATOR + "learning" + SEPARATOR + userId + SEPARATOR + courseId;
    }


    /**
     * 用户课程进度列表key
     */
    public static String getUserCourseProgressListKey(Integer userId) {
        return PREFIX_USER + SEPARATOR + "progress_list" + SEPARATOR + userId;
    }

    /**
     * 获取课程搜索索引key
     * <p>
     * <p>
     * /**
     * 课程学习时长key
     */
    public static String getCourseStudyTimeKey(Integer courseId) {
        return PREFIX_COURSE + SEPARATOR + "study_time" + SEPARATOR + courseId;
    }

    /**
     * 获取最新课程缓存key
     */
    public static String getLatestCoursesKey(int limit) {
        return PREFIX_COURSE + SEPARATOR + "latest" + SEPARATOR + limit;
    }

    /**
     * 获取免费课程缓存key
     */
    public static String getFreeCoursesKey(int page, int size) {
        return PREFIX_COURSE + SEPARATOR + "free" + SEPARATOR + page + SEPARATOR + size;
    }

    /**
     * 获取价格区间课程缓存key
     */
    public static String getPriceRangeCoursesKey(Double minPrice, Double maxPrice, int page, int size) {
        return PREFIX_COURSE + SEPARATOR + "price_range" + SEPARATOR + minPrice + SEPARATOR + maxPrice + SEPARATOR + page + SEPARATOR + size;
    }



/**
     * 总学习时长排行榜key
     */
    public static String getTotalStudyTimeKey() {
        return PREFIX_LEADERBOARD + SEPARATOR + "total_study_time";
    }

    /**
     * 用户已完成课程key
     */
    public static String getUserCompletedCoursesKey(Integer userId) {
        return PREFIX_USER + SEPARATOR + "completed_courses" + SEPARATOR + userId;
    }

    /**
     * 用户总学习时长key
     */
    public static String getUserTotalStudyTimeKey(Integer userId) {
        return PREFIX_USER + SEPARATOR + "total_study_time" + SEPARATOR + userId;
    }

    /**
     * 学习时长key（用户或课程）
     */
    public static String getStudyTimeKey(Integer userId, Integer courseId) {
        if (courseId != null) {
            return PREFIX_USER + SEPARATOR + "study_time" + SEPARATOR + userId + SEPARATOR + courseId;
        } else {
            return PREFIX_USER + SEPARATOR + "study_time" + SEPARATOR + userId;
        }
    }

    /**
     * 用户学习时长排行榜key
     */
    public static String getUserStudyTimeRankKey() {
        return PREFIX_LEADERBOARD + SEPARATOR + "user_study_time";
    }


    /**
     * 用户课程学习时长
     */
    public static String getUserCourseStudyDurationKey(Integer userId, Integer courseId) {
        return PREFIX_USER + SEPARATOR + "course_duration" + SEPARATOR + userId + SEPARATOR + courseId;
    }

    /**
     * 获取课程搜索索引key
     */
    public static String getCourseSearchIndexKey() {
        return PREFIX_COURSE + SEPARATOR + "search" + SEPARATOR + "index";
    }

    /**
     * 获取课程列表缓存key
     */
    public static String getCourseListKey(int page, int size) {
        return PREFIX_COURSE + SEPARATOR + "list" + SEPARATOR + page + SEPARATOR + size;
    }

    /**
     * 获取课程搜索缓存key
     */
    public static String getCourseSearchKey(String keyword, String category, int page, int size) {
        String key = PREFIX_COURSE + SEPARATOR + "search" + SEPARATOR + keyword;
        if (category != null) {
            key += SEPARATOR + category;
        }
        return key + SEPARATOR + page + SEPARATOR + size;
    }

    /**
     * 获取课程分类缓存key
     */
    public static String getCourseCategoryKey(Integer categoryId, int page, int size) {
        return PREFIX_COURSE + SEPARATOR + "category" + SEPARATOR + categoryId + SEPARATOR + page + SEPARATOR + size;
    }

    /**
     * 获取热门课程缓存key
     */
    public static String getHotCoursesKey(int limit) {
        return PREFIX_COURSE + SEPARATOR + "hot" + SEPARATOR + limit;
    }

    /**
     * 获取高分课程缓存key
     */
    public static String getTopRatedCoursesKey(int limit) {
        return PREFIX_COURSE + SEPARATOR + "top" + SEPARATOR + "rating" + SEPARATOR + limit;
    }

    /**
     * 获取热销课程缓存key
     */
    public static String getTopSellingCoursesKey(int limit) {
        return PREFIX_COURSE + SEPARATOR + "top" + SEPARATOR + "selling" + SEPARATOR + limit;
    }

    /**
     * 获取浏览最多课程缓存key
     */
    public static String getMostViewedCoursesKey(int limit) {
        return PREFIX_COURSE + SEPARATOR + "top" + SEPARATOR + "viewed" + SEPARATOR + limit;
    }

    /**
     * 获取课程总数缓存key
     */
    public static String getCourseCountKey() {
        return PREFIX_COURSE + SEPARATOR + "count";
    }

    /**
     * 获取浏览量排行榜key
     */
    public static String getViewsLeaderboardKey() {
        return PREFIX_COURSE + SEPARATOR + "leaderboard" + SEPARATOR + "views";
    }

    /**
     * 获取销量排行榜key
     */
    public static String getSalesLeaderboardKey() {
        return PREFIX_COURSE + SEPARATOR + "leaderboard" + SEPARATOR + "sales";
    }

    /**
     * 获取评分排行榜key
     */
    public static String getRatingLeaderboardKey() {
        return PREFIX_COURSE + SEPARATOR + "leaderboard" + SEPARATOR + "rating";
    }

    /**
     * 获取搜索结果总数缓存key
     */
    public static String getSearchCountKey(String keyword, String category) {
        String key = PREFIX_COURSE + SEPARATOR + "search" + SEPARATOR + "count" + SEPARATOR + keyword;
        if (category != null) {
            key += SEPARATOR + category;
        }
        return key;
    }

    /**
     * 获取分类课程数量缓存key
     */
    public static String getCategoryCountKey(Integer categoryId) {
        return PREFIX_COURSE + SEPARATOR + "category" + SEPARATOR + "count" + SEPARATOR + categoryId;
    }


    /**
     * 令牌桶限流key
     */
    public static String getTokenBucketKey(String apiKey) {
        return PREFIX_RATE_LIMIT + SEPARATOR + "token_bucket" + SEPARATOR + apiKey;
    }

    /**
     * 全局API限流key
     */
    public static String getGlobalApiRateLimitKey(String apiKey) {
        return PREFIX_RATE_LIMIT + SEPARATOR + "global_api" + SEPARATOR + apiKey;
    }

}